home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / libs / ttengine.lha / ttengine-4.1 / Examples / TextExtent / txex.c < prev   
C/C++ Source or Header  |  2002-09-29  |  6KB  |  162 lines

  1. /* test ttengine */
  2.  
  3. #define __NOLIBBASE__
  4.  
  5. #include <proto/dos.h>
  6. #include <proto/exec.h>
  7. #include <proto/intuition.h>
  8. #include <proto/graphics.h>
  9. #include <proto/ttengine.h>
  10. #include <proto/asl.h>
  11.  
  12. #include <libraries/ttengine.h>
  13.  
  14. extern struct Library *SysBase, *DOSBase;
  15.  
  16. struct Library *TTEngineBase, *IntuitionBase, *GfxBase, *AslBase;
  17.  
  18. /*----------------------------------------------------------------------------------------------------*/
  19.  
  20. static STRPTR get_font_name(struct Library *AslBase)
  21.   {
  22.     struct FileRequester *freq;
  23.     STRPTR name = NULL;
  24.  
  25.     if (freq = AllocAslRequestTags(ASL_FileRequest, TAG_END))
  26.       {
  27.         if (AslRequestTags(freq,
  28.           ASLFR_TitleText, (ULONG)"Select TrueType font",
  29.           ASLFR_InitialDrawer, (ULONG)"FONTS:",
  30.           ASLFR_DoPatterns, TRUE,
  31.           ASLFR_InitialPattern, (ULONG)"#?.ttf",
  32.           ASLFR_RejectIcons, TRUE,
  33.           TAG_END))
  34.           {
  35.             ULONG namelen = strlen(freq->fr_File) + strlen(freq->fr_Drawer) + 4;
  36.  
  37.             if (name = AllocVec(namelen + 1, MEMF_ANY | MEMF_CLEAR))
  38.               {
  39.                 strncpy(name, freq->fr_Drawer, namelen);
  40.                 AddPart(name, freq->fr_File, namelen);
  41.               }
  42.           }
  43.         FreeAslRequest(freq);
  44.       }
  45.     return name;
  46.   }
  47.  
  48. /*----------------------------------------------------------------------------------------------------*/
  49.  
  50. static VOID free_font_name(STRPTR name)
  51.   {
  52.     if (name) FreeVec(name);
  53.   }
  54.  
  55. /*----------------------------------------------------------------------------------------------------*/
  56.  
  57. void framed_text(STRPTR text, UWORD y, struct RastPort *rp)
  58.   {
  59.     struct TextExtent te;
  60.     UWORD len;
  61.  
  62.     len = strlen(text);
  63.  
  64.     TT_TextExtent(rp, text, len, &te);
  65.  
  66.     SetAPen(rp, 2);
  67.     Move(rp, 10 + te.te_Extent.MinX, y + te.te_Extent.MaxY);
  68.     Draw(rp, 10 + te.te_Extent.MinX, y + te.te_Extent.MinY);
  69.     Draw(rp, 10 + te.te_Extent.MaxX, y + te.te_Extent.MinY);
  70.     Draw(rp, 10 + te.te_Extent.MaxX, y + te.te_Extent.MaxY);
  71.     Draw(rp, 10 + te.te_Extent.MinX, y + te.te_Extent.MaxY);
  72.  
  73.     SetAPen(rp, 1);
  74.     Move(rp, 10, y);
  75.     TT_Text(rp, text, len);
  76.   }
  77.  
  78.  
  79. int Main (void)
  80.   {
  81.     struct Window *win;
  82.     STRPTR fontname;
  83.     APTR font;
  84.  
  85.     if (GfxBase = OpenLibrary("graphics.library", 39))
  86.       {
  87.         if (IntuitionBase = OpenLibrary("intuition.library", 39))
  88.           {
  89.             if (AslBase = OpenLibrary("asl.library", 38))
  90.               {
  91.                 if (fontname = get_font_name(AslBase))
  92.                   {
  93.                     if (TTEngineBase = OpenLibrary("ttengine.library", 0))
  94.                       {
  95.                         if (win = OpenWindowTags(NULL,
  96.                           WA_Top, 25,
  97.                           WA_Left, 0,
  98.                           WA_Width, 640,
  99.                           WA_Height, 210,
  100.                           WA_CloseGadget, TRUE,
  101.                           WA_DragBar, TRUE,
  102.                           WA_DepthGadget, TRUE,
  103.                           WA_IDCMP, IDCMP_CLOSEWINDOW,
  104.                           WA_Title, (ULONG)"TT_TextExtent() test",
  105.                           TAG_END))
  106.                           {
  107.                             ULONG sigmask, signals;
  108.                             BOOL running = TRUE;
  109.                             struct RastPort *rp = win->RPort;
  110.  
  111.                             if (font = TT_OpenFont(
  112.                               TT_FontFile, (ULONG)fontname,
  113.                               TT_FontSize, 31,
  114.                             TAG_END))
  115.                               {
  116.                                 TT_SetFont(rp, font);
  117.                                 TT_SetAttrs(rp,
  118.                                   TT_Window, (ULONG)win,
  119.                                   TT_Antialias, TT_Antialias_On,
  120.                                 TAG_END);
  121.  
  122.                                 SetDrMd(rp, JAM1);
  123.  
  124.                                 framed_text("TT_TextExtent() example", 46, rp);
  125.                                 framed_text("Vjgq2837!^²³_&##,.P", 92, rp);
  126.                                 framed_text("Frame should tightly enclose the text.", 138, rp);
  127.  
  128.                                 TT_CloseFont(font);
  129.                               }
  130.                             else PutStr("Font open failed.\n");
  131.  
  132.                             sigmask = SIGBREAKF_CTRL_C | (1 << win->UserPort->mp_SigBit);
  133.                             while (running)
  134.                               {
  135.                                 signals = Wait(sigmask);
  136.                                 if (signals & SIGBREAKF_CTRL_C) running = FALSE;
  137.                                 if (signals & (1 << win->UserPort->mp_SigBit))
  138.                                   {
  139.                                     struct IntuiMessage *imsg;
  140.  
  141.                                     while (imsg = (struct IntuiMessage*)GetMsg(win->UserPort))
  142.                                       {
  143.                                         if (imsg->Class == IDCMP_CLOSEWINDOW) running = FALSE;
  144.                                         ReplyMsg((struct Message*)imsg);
  145.                                       }
  146.                                   }
  147.                               }
  148.                             CloseWindow(win);
  149.                           }
  150.                         CloseLibrary(TTEngineBase);
  151.                       }
  152.                     free_font_name(fontname);
  153.                   }
  154.                 CloseLibrary(AslBase);
  155.               }
  156.             CloseLibrary(IntuitionBase);
  157.           }
  158.         CloseLibrary(GfxBase);
  159.       }
  160.     return 0;
  161.   }
  162.